package com.jhgk.jhcee.forecast.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jhgk.jhcee.bfk.mapper.MPreScoreZjbExportMapper;
import com.jhgk.jhcee.bfk.model.MPreScoreZjbExport;
import com.jhgk.jhcee.bfk.model.MPreScoreZjbExportExample;
import com.jhgk.jhcee.common.enums.BasisResultEnum;
import com.jhgk.jhcee.common.resultful.BasisResultful;
import com.jhgk.jhcee.common.resultful.PageResultful;
import com.jhgk.jhcee.forecast.domain.model.MPreScoreZjbExportMode;
import com.jhgk.jhcee.forecast.service.MPreScoreZjbExportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: xiezhongping
 * @description:
 * @date: 2021/8/10 15:07.
 */
@Service(value = "MPreScoreZjbExportServiceImpl")
public class MPreScoreZjbExportServiceImpl implements MPreScoreZjbExportService {

    @Autowired
    MPreScoreZjbExportMapper mPreScoreZjbExportMapper;

    @Override
    public PageResultful<PageInfo<MPreScoreZjbExport>> searchAllPage(MPreScoreZjbExportMode mPreScoreZjbExportMode) {
        if (null == mPreScoreZjbExportMode) {
            return PageResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        MPreScoreZjbExportExample example = new MPreScoreZjbExportExample();
        MPreScoreZjbExportExample.Criteria criteria = example.createCriteria();
        criteria.andProvinceUuidEqualTo(mPreScoreZjbExportMode.getProvinceUuid());
        criteria.andYearEqualTo(mPreScoreZjbExportMode.getYear());
        PageHelper.startPage(mPreScoreZjbExportMode.getPage(), mPreScoreZjbExportMode.getLimit());
        List<MPreScoreZjbExport> mPreScoreZjbExports = mPreScoreZjbExportMapper.selectByExample(example);
        if (mPreScoreZjbExports.isEmpty()) {
            return PageResultful.fail(BasisResultEnum.QUERY_IS_NULL);
        }
        PageInfo<MPreScoreZjbExport> pageInfo = new PageInfo<>(mPreScoreZjbExports);
        return PageResultful.success(pageInfo);
    }

    @Override
    public BasisResultful<Object> searclTotal(String provinceUuid, Integer year) {
        if (StringUtils.isBlank(provinceUuid) || null == year) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        MPreScoreZjbExportExample example = new MPreScoreZjbExportExample();
        MPreScoreZjbExportExample.Criteria criteria = example.createCriteria();
        criteria.andProvinceUuidEqualTo(provinceUuid);
        criteria.andYearEqualTo(year);
        PageHelper.startPage(1,1);
        List<MPreScoreZjbExport> mPreScoreZjbExports = mPreScoreZjbExportMapper.selectByExample(example);
        if (mPreScoreZjbExports.isEmpty()) {
            return BasisResultful.fail(BasisResultEnum.QUERY_IS_NULL);
        }
        PageInfo<MPreScoreZjbExport> pageInfo = new PageInfo<>(mPreScoreZjbExports);
        return BasisResultful.success(pageInfo.getTotal());
    }
}
